"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : gs_dump工具导出兼容M*建表添加可选项语法
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库下建表，指定engine、row_format选项
    3.兼容b库下建表，指定compression设置压缩参数
    4.兼容b库下建表，支持表级、字段级character和collate的语法兼容
    5.使用gs_dump工具导出表为纯文本格式
    6.导入之前导出的数据到新库
    7.连接导入库查询表信息
    8.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功，表导入成功
    8.成功
History     :modified by z@wan005，2023.08.18，修改step6
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0013(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.db_name = "db_dump_0013"
        self.db_name_01 = "db_dump_0013_01"
        self.tb_name = "tb_dump_0013"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0013')
        self.default_value = self.com.show_param('support_extended_features')

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('---修改参数为on---')
        msg = self.sh_primary.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'support_extended_features = on')
        self.log.info(msg)
        self.assertTrue(msg)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} encoding 'UTF8' " \
                  f"dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} encoding 'UTF8' " \
                  f"dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败:' + text)

        text = '---step2: 兼容b库下建表，指定engine、row_format选项 expect: 成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(c1 int primary key auto_increment \
        comment '主键')\
        engine ='引擎' \
        row_format test_row_format;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '---step3: 兼容b库下建表，指定compression设置压缩参数 expect: 成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name}_01;
        create table {self.tb_name}_01(c int) compression = pglz;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '---step4: 兼容b库下建表，支持表级、字段级character和collate的语法兼容' \
               ' expect: 成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name}_02,\
        {self.tb_name}_03,{self.tb_name}_04,{self.tb_name}_05,\
        {self.tb_name}_06,{self.tb_name}_07,{self.tb_name}_08;
        create table {self.tb_name}_02(c text character set \
        test_character_set);
        create table {self.tb_name}_03(c text charset test_charset);
        create table {self.tb_name}_04(c text collate utf8mb4_bin);
        create table {self.tb_name}_05(c int) character set utf8;
        create table {self.tb_name}_06(c int) charset utf8;
        create table {self.tb_name}_07(c int) collate utf8mb4_general_ci;
        create table {self.tb_name}_08(c int) character set utf8 collate \
        utf8mb4_general_ci;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertEqual(7,
                         sql_result.count(self.constant.TABLE_CREATE_SUCCESS),
                         '执行失败:' + text)

        text = '----step5: 使用gs_dump工具导出表为纯文本格式 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dump " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"{self.db_name} -F p " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step6: 导入之前导出的数据到新库 expect: 成功----'
        self.log.info(text)
        gsql_cmd = f"source {macro.DB_ENV_PATH};" \
                   f"gsql -p {self.dbuser_node.db_port} " \
                   f"{self.db_name_01} -r -f {self.dump_path}"
        self.log.info(gsql_cmd)
        gsql_msg = self.dbuser_node.sh(gsql_cmd).result()
        self.log.info(gsql_msg)
        self.assertEqual(9, gsql_msg.count('CREATE TABLE'),
                         '执行失败:' + text)

        text = '-step7: 连接导入库查询表信息 expect: 成功，表导入成功，表的可选项不显示--'
        self.log.info(text)
        sql_cmd = f"show create table {self.tb_name};" \
                  f"show create table {self.tb_name}_01;" \
                  f"show create table {self.tb_name}_02;" \
                  f"show create table {self.tb_name}_03;" \
                  f"show create table {self.tb_name}_04;" \
                  f"show create table {self.tb_name}_05;" \
                  f"show create table {self.tb_name}_06;" \
                  f"show create table {self.tb_name}_07;" \
                  f"show create table {self.tb_name}_08;"
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn("COMMENT ON COLUMN tb_dump_0013.c1 IS '主键'",
                      sql_result, '执行失败:' + text)
        self.assertIn('compresstype=1', sql_result, '执行失败:' + text)
        self.assertIn('c text CHARACTER SET "UTF8" COLLATE utf8mb4_bin',
                      sql_result, '执行失败:' + text)
        self.assertEqual(4, sql_result.count('CHARACTER SET = "UTF8" COLLATE '
                                             '= "utf8mb4_general_ci"'),
                         '执行失败:' + text)

    def tearDown(self):
        text = '---step8：清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        msg = self.sh_primary.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'support_extended_features '
                                            f'= {self.default_value}')
        self.log.info(msg)
        self.assertTrue(msg)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertEqual(2,
                         sql_result.count(self.constant.DROP_DATABASE_SUCCESS))
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
